﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Prop
{
    public partial class Form1 : Form
    {
         DataHelper dh;
         Shop shop1;
         double total;
        public Form1()
        {
            InitializeComponent();
            dh = new DataHelper();
            shop1 = new Shop(1);
            loadItems();
            
        }
        private void loadItems()
        {
            listBox2.Items.Clear();
            List<Item> l = shop1.getAllItems();
            for (int i = 0; i < l.Count; i++)
            {
              if (l[i].GetType().ToString() == "Prop.Food")
                {
                listBox3.Items.Add(l[i].getInfo());
                }
              else   if(l[i].GetType().ToString() == "Prop.Drink")
                listBox2.Items.Add(l[i].getInfo());
            }
            
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void btnVisitor_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
           Visitor v;
           v = dh.getVisitor(Convert.ToInt32(tbId.Text));
           
           listBox1.Items.Add(v.getId());
           listBox1.Items.Add(v.getName());
           listBox1.Items.Add(v.getBalance());
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listBox2_MouseDown(object sender, MouseEventArgs e)
        {
            listBox2.SelectedIndex = listBox2.IndexFromPoint(e.X, e.Y);
            //string curItem = listBox2.SelectedItem.ToString();
            string curItem = listBox2.Items[listBox2.SelectedIndex].ToString();
            Item r;

            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                //....
                string[] arr;
                arr = curItem.Split(',');         // sperated by ","
                int prodId = Convert.ToInt32(arr[0]);

                int index = listBox3.FindString(prodId.ToString());
                listBox3.Items.RemoveAt(index);

                r = shop1.getItem(prodId);
                r.NumInStock++;
                loadItems();
                
                total -= r.Price;

                label8.Text = "" + total;
                
                

            }
            else
                if (e.Button == System.Windows.Forms.MouseButtons.Left)
                {
                    //...
                    //listBox3.Items.Add(curItem);
                    string[] arr;
                    arr = curItem.Split(',');         // sperated by ","
                    listBox3.Items.Add(arr[0] + "," + arr[1]);
                    int prodId = Convert.ToInt32(arr[0]);
                    r = shop1.getItem(prodId);
                    r.NumInStock--;

                    loadItems();
                   
                    
                    total += r.Price * Convert.ToDouble(numericUpDown1.Value);
                    
                    label8.Text = "" + total;
                   
                    

                }
        }

        
        }
    }

